package com.dyb.recuitment.controller;


import com.dyb.recuitment.common.Result;
import com.dyb.recuitment.entity.Job;
import com.dyb.recuitment.entity.Resume;
import com.dyb.recuitment.entity.ResumeDelivery;
import com.dyb.recuitment.entity.User;
import com.dyb.recuitment.service.IResumeDeliveryService;
import com.dyb.recuitment.service.IResumeService;
import com.dyb.recuitment.service.IUserLogService;
import com.dyb.recuitment.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 简历投递表 前端控制器
 * </p>
 *
 * @author 邓宇斌
 * @since 2022-02-22
 */
@RestController
@Api(tags = "简历投递管理(ResumeDelivery)")
@RequestMapping("/resume_delivery")
public class ResumeDeliveryController {

    @Autowired
    private IResumeDeliveryService resumeDeliveryService;

    @Autowired
    private IResumeService resumeService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IUserLogService userLogService;

    @ApiOperation(value = "添加简历投递", notes = "通过用户ID、岗位ID添加简历模块 " +
            "{\"userId\":1,\"jobId\":1}")
    @ApiImplicitParam(name = "resumeDelivery", value = "ResumeDelivery类")
    @PostMapping("/add")
    public Object add(@RequestBody ResumeDelivery resumeDelivery) {
        Object data = null;
        try {
            data = resumeDeliveryService.add(resumeDelivery);
            if (data == null || (int) data == 0) {
                return Result.warning(data, "当前岗位已投递!");
            }
            return Result.success(data, "添加成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "查看简历", notes = "通过ID获取简历 " +
            "{\"id\":1}")
    @ApiImplicitParam(name = "resumeDelivery", value = "ResumeDelivery类")
    @PostMapping("/resumeDetail")
    public Object resumeDetail(@RequestBody ResumeDelivery resumeDelivery) {
        Object[] data = new Object[2];
        try {
            ResumeDelivery result = resumeDeliveryService.getById(resumeDelivery);
            if (result.getFileId() == 0) {
                User user = new User();
                Resume resume = new Resume();

                user.setId(result.getUserId());
                resume.setCreateBy(result.getUserId());

                data[0] = userService.getByUser(user);
                data[1] = resumeService.selectByCreateBy(resume);
                if (data[0] == null || data[1] == null) {
                    return Result.warning(data, "查看失败");
                }
            } else {

            }

            return Result.success(data, "查看成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "撤回简历投递", notes = "通过ID撤回简历投递 " +
            "{\"id\":1}")
    @ApiImplicitParam(name = "resumeDelivery", value = "ResumeDelivery类")
    @PostMapping("/delete")
    public Object delete(@RequestBody ResumeDelivery resumeDelivery) {
        Object data = null;
        try {
            data = resumeDeliveryService.delete(resumeDelivery);
            if (data == null || data == (Integer) 0) {
                return Result.failure(data, "撤回失败!");
            }
            return Result.success(data, "撤回成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "按条件查询简历投递", notes = "按条件查询简历投递 {\"userId\":1}")
    @ApiImplicitParam(name = "resumeDelivery", value = "ResumeDelivery类")
    @PostMapping("/selectByResumeDelivery")
    public Object selectByResumeDelivery(@RequestBody ResumeDelivery resumeDelivery) {
        Object data = null;
        try {
            data = resumeDeliveryService.selectByResumeDelivery(resumeDelivery);
            if (data == null || ((List) data).size() <= 0) {
                return Result.warning(data, "暂无简历投递情况!");
            }
            return Result.success(data, "查询成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }

    @ApiOperation(value = "查询所有简历投递", notes = "查询所有简历投递")
    @PostMapping("/selectAllResumeDelivery")
    public Object selectAllResumeDelivery() {
        Object data = null;
        try {
            data = resumeDeliveryService.selectAllResumeDelivery();
            if (data == null || ((List) data).size() <= 0) {
                return Result.warning(data, "暂无简历投递情况!");
            }
            return Result.success(data, "查询成功!");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return Result.failure(data);
    }


}
